"""
爬虫任务记录映射器
"""
from typing import List, Optional
from axiom_boot.database.base_mapper import BaseMapper
from axiom_boot.di import mapper

from ..models.scraper_task_record import ScraperTaskRecord


@mapper()
class ScraperTaskRecordMapper(BaseMapper[ScraperTaskRecord]):
    """爬虫任务记录映射器"""
    
    def __init__(self):
        super().__init__()
    
    async def find_by_task_id(self, task_id: str) -> Optional[ScraperTaskRecord]:
        """根据任务ID查找记录"""
        results = await self.find_by_filters(task_id=task_id, limit=1)
        return results[0] if results else None
    
    async def find_running_tasks(self) -> List[ScraperTaskRecord]:
        """查找正在运行的任务"""
        return await self.find_by_filters(
            task_status="running",
            order_by=["-start_time"]
        )
    
    async def find_by_site(self, target_site: str, limit: int = 10) -> List[ScraperTaskRecord]:
        """根据目标站点查找任务"""
        return await self.find_by_filters(
            target_site=target_site,
            limit=limit,
            order_by=["-start_time"]
        )